<template>
  <div class="experience-create">
    <div class="page-header">
      <h1>分享面试经验</h1>
      <p class="subtitle">分享你的面试经历，帮助他人获得宝贵经验</p>
    </div>

    <el-card class="create-card" shadow="never">
      <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        label-width="100px"
        label-position="top"
      >
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="面试公司" prop="company_name">
              <el-input
                v-model="formData.company_name"
                placeholder="请输入公司名称"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="面试岗位" prop="position">
              <el-input
                v-model="formData.position"
                placeholder="请输入岗位名称"
                clearable
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="是否匿名">
          <el-switch
            v-model="formData.is_anonymous"
            :active-value="1"
            :inactive-value="0"
            active-text="匿名发布"
            inactive-text="实名发布"
          />
          <div class="form-tip">匿名发布将不会显示你的用户信息</div>
        </el-form-item>

        <el-form-item label="面试经验" prop="experience_content">
          <el-input
            v-model="formData.experience_content"
            type="textarea"
            :rows="8"
            placeholder="请详细描述你的面试经历，包括面试流程、面试官态度、公司环境等..."
            show-word-limit
            maxlength="2000"
          />
        </el-form-item>

        <el-form-item label="面试题目（选填）">
          <el-input
            v-model="formData.interview_questions"
            type="textarea"
            :rows="6"
            placeholder="请列出面试中遇到的问题，每个问题一行&#10;例如：&#10;1. 自我介绍&#10;2. 项目经验介绍&#10;3. 技术问题..."
            show-word-limit
            maxlength="1000"
          />
        </el-form-item>

        <el-form-item>
          <div class="form-actions">
            <el-button type="primary" size="large" @click="handleSubmit" :loading="submitting">
              <el-icon><Document /></el-icon>
              发布经验
            </el-button>
            <el-button size="large" @click="handleCancel">
              取消
            </el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Document } from '@element-plus/icons-vue'
import { interviewAPI } from '@/api/interview'

const router = useRouter()
const formRef = ref(null)
const submitting = ref(false)

const formData = reactive({
  company_name: '',
  position: '',
  is_anonymous: 0,
  experience_content: '',
  interview_questions: ''
})

const formRules = {
  company_name: [
    { required: true, message: '请输入公司名称', trigger: 'blur' },
    { min: 2, max: 100, message: '公司名称长度在 2 到 100 个字符', trigger: 'blur' }
  ],
  position: [
    { required: true, message: '请输入岗位名称', trigger: 'blur' },
    { min: 2, max: 100, message: '岗位名称长度在 2 到 100 个字符', trigger: 'blur' }
  ],
  experience_content: [
    { required: true, message: '请输入面试经验', trigger: 'blur' },
    { min: 20, max: 2000, message: '面试经验长度在 20 到 2000 个字符', trigger: 'blur' }
  ]
}

const handleSubmit = async () => {
  try {
    const valid = await formRef.value.validate()
    if (!valid) return

    submitting.value = true

    const response = await interviewAPI.createExperience(formData)
    
    if (response.code === 200) {
      ElMessage.success('发布成功！')
      router.push('/interview-experience')
    } else {
      ElMessage.error(response.message || '发布失败')
    }
  } catch (error) {
    console.error('发布失败:', error)
    if (error !== false) { // 排除表单验证失败的情况
      ElMessage.error('发布失败，请重试')
    }
  } finally {
    submitting.value = false
  }
}

const handleCancel = () => {
  router.back()
}
</script>

<style scoped>
.experience-create {
  max-width: 900px;
  margin: 0 auto;
  padding: 20px;
}

.page-header {
  margin-bottom: 30px;
}

.page-header h1 {
  margin: 0 0 10px 0;
  color: #333;
  font-size: 28px;
  font-weight: 600;
}

.subtitle {
  margin: 0;
  color: #666;
  font-size: 14px;
}

.create-card {
  border-radius: 12px;
  border: none;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
}

:deep(.el-card__body) {
  padding: 30px;
}

.form-tip {
  font-size: 12px;
  color: #999;
  margin-top: 5px;
}

.form-actions {
  display: flex;
  gap: 12px;
  padding-top: 20px;
  border-top: 1px solid #f0f0f0;
}

:deep(.el-form-item__label) {
  font-weight: 500;
  color: #333;
}

:deep(.el-textarea__inner) {
  font-family: inherit;
  line-height: 1.6;
}

:deep(.el-input__inner),
:deep(.el-textarea__inner) {
  border-radius: 8px;
}
</style>

